export default {
  namespaced: true,
  state: () => ({
    items: [
    ],
    total: 555, // 总价格
    allChecked: true // 是否全选
  }),
  mutations: {
    calc_total (state) {
      /*
      所有已经选中的商品的 价格 * 数量 总和
      */
      let total = 0
      const checkItems = state.items.filter(item => item.checked)
      checkItems.forEach(item => {
        total += item.num * item.minPrice
      })
      state.total = parseFloat(total.toFixed(2))
    },
    updata_items (state, items) {
      state.items = items
    },
    set_single_checked (state, { index, checked }) {
      // 改变商品选中状态
      state.items[index].checked = checked
    },
    set_allChecked (state, checked) {
      // 修改全选状态
      state.allChecked = checked
    },
    set_all_single_checked (state, checked) {
      // 全选状态改变影响单选
      state.items.forEach(item => { item.checked = checked })
    },
    del_item (state, index) {
      state.items.splice(index, 1)
    },
    add_cart (state, item) {
      // 加入购物车
      state.items.unshift(item)
    }
  }
}
